package com.bitBusy.manageyourriches.database.queries;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

import com.bitBusy.manageyourriches.R;
import com.bitBusy.manageyourriches.database.tables.PaymentInfo;
import com.bitBusy.manageyourriches.utilityClasses.dialogboxes.DialogBoxPrompts;
import com.bitBusy.manageyourriches.utilityClasses.display.DisplayMessages;

/**
 * Class that holds all queries related to the CategoryImages table. 
 * @extends the DatabaseQueries class*/
public class QueriesPaymentInfoTable extends DatabaseQueries{
	
	/** Name of table related to queries in this class*/
	private static final String tableName = PaymentInfo.class.getSimpleName();
	
	/** String for the method column name*/
	private static final String METHODCOL = PaymentInfo.METHODCOL.getName();
			
	/** String for the pic id column name*/
	private static final String picIDCol = PaymentInfo.PICIDCOL.getName();
			
	/** String array of all column names in this table*/
	private static final String[] allColumns = new String[]{METHODCOL, picIDCol};
	
	/** Reference to the Cursor object used to store and process query results*/
	private Cursor cursor;

	/** Parameterized constructor - calls the superclass constructor
	 * @param the_context the database context
	 * @param the current activity context
	 */
	public QueriesPaymentInfoTable(Context the_context) {
		super(the_context);
	}

	/** Method that returns all pairs of method names and pic IDs in the table
	 * QueriesBasicDetailsTable.getSubcategories()
	 * @return map of method names as key and pic ids as value
	 */
	public LinkedHashMap<String, Integer> getAllPaymentMethodsAndImages()
	{
		boolean iOpened = checkAndOpen();
		
		LinkedHashMap<String, Integer> methodImage = null;
		String methodName;
	    int picID = 0;
		cursor = myDatabase.query
		(tableName,allColumns, null, null, null, null, null);
		if (cursor != null && cursor.moveToFirst())
		{
			methodImage = new LinkedHashMap<String, Integer>();
			while (!cursor.isAfterLast())
			{
				//categoryImge.add(cursorToCategory(cursor));
				methodName = cursor.getString((cursor.getColumnIndex(METHODCOL)));
				try
				{
					picID = Integer.parseInt(cursor.getString(cursor.getColumnIndex(picIDCol)));
				}
				catch(NumberFormatException ne)
				{
					DisplayMessages.displayUserError(myActivityContext, DialogBoxPrompts.INVALIDPICIDPROMPT.getPrompt());
					picID = R.drawable.paymentmethod;

				}
				methodImage.put(methodName, picID);
				cursor.moveToNext();
			}
			cursor.close();
		}
		checkAndClose(iOpened);
		return methodImage;
	}
	

	
	public List<String> getAllImages()
	{
		return null;
	}
	
		
	public void addPaymentMethods(Map<String, Integer> the_methodImageMap)
	{
		//database = the_db;
		boolean iOpened = checkAndOpen();
		if (myDatabase != null && the_methodImageMap != null )
		{
			try
				{
				ContentValues content = new ContentValues();
				for (Map.Entry<String, Integer> entry: the_methodImageMap.entrySet())
				{
					content.put(METHODCOL, entry.getKey());
					content.put(picIDCol, entry.getValue());
					myDatabase.insertOrThrow(tableName, null, content);
				}
				}
				catch(SQLiteException exception)
				{
					DisplayMessages.displayUserError(myActivityContext, DialogBoxPrompts.DIALOGNONUNIQUECATNAME.getPrompt());
				}
				finally
				{
					checkAndClose(iOpened);
				}
		}
		else
		{
			DisplayMessages.displayInternalError(myActivityContext, DialogBoxPrompts.DIALOGNULLVALUERECEIVEDPROMPT.getPrompt());
		}
}

	/**
	 * Used only during initialization of table
	 * @param methodImages map
	 * @param the_db
	 */
	public void addCategories(Map<String, Integer> the_methodImageMap,
			SQLiteDatabase the_db) {
		if (the_methodImageMap != null && the_db != null)
		{
			myDatabase = the_db;
			try
			{
		
				ContentValues content = new ContentValues();
				for (Map.Entry<String, Integer> entry: the_methodImageMap.entrySet())
				{
					content.put(METHODCOL, entry.getKey());
					content.put(picIDCol, entry.getValue());
					myDatabase.insertOrThrow(tableName, null, content);
		
				}
			}
			catch(SQLiteException exception)
			{
				DisplayMessages.displayUserError(myActivityContext, DialogBoxPrompts.DIALOGNONUNIQUECATNAME.getPrompt());
			}
	}
		else
		{
			DisplayMessages.displayInternalError(myActivityContext, DialogBoxPrompts.DIALOGNULLVALUERECEIVEDPROMPT.getPrompt());

		}
		
	}
}
	

